contour detection algorithm